package com.attcatstudy.cas;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.stream.IntStream;

/**
 * @author attcat
 * ---做一个有趣的程序员
 * @date 2022-11-02 21:29
 * @description
 */
public class LongAccumulatorTest {
    public static void main(String[] args) throws InterruptedException {
        LongAccumulator accumulator = new LongAccumulator((x, y) -> x + y, 0);

        ExecutorService executor = Executors.newFixedThreadPool(8);
        // 1到9累加
        IntStream.range(1, 10).forEach(i -> executor.submit(() ->
                accumulator.accumulate(i)));

        Thread.sleep(2000); System.out.println(accumulator.getThenReset());
        executor.shutdown();
    }

}
